package mo.golden;

import java.util.*;

public class L03_03 {

    private int cap;
    private List<Stack<Integer>> stacks = new ArrayList<>();

    public L03_03(int cap) {
        this.cap = cap;
    }

    public void push(int val) {
        if (cap <= 0) {
            return;
        }
        if (stacks.isEmpty() || stacks.get(stacks.size()-1).size() == cap) {
            stacks.add(new Stack<>());
        }
        stacks.get(stacks.size()-1).add(val);
    }

    public int pop() {
        if (stacks.size() == 0) {
            return -1;
        }
        int idx = stacks.size()-1;
        int result = stacks.get(idx).pop();
        if (stacks.get(idx).size() == 0) {
            stacks.remove(idx);
        }
        return result;
    }

    public int popAt(int index) {
        if (index < 0 || stacks.size() == 0 || index >= stacks.size()) {
            return -1;
        }
        Stack<Integer> stack = stacks.get(index);
        int result = stack.pop();
        if (stack.size() == 0) {
            stacks.remove(index);
        }
        return result;
    }



}
